#include <gtest/gtest.h>
#include <vector>
#include "record.h"
#include "database.h"

// 测试 fixture，每个测试前初始化，测试后清理
// class DatabaseTest : public ::testing::Test {
// protected:
//     SimpleDatabase db;
//     const std::string db_name = "test_db";

//     DatabaseTest() : db(db_name) {}

//     void SetUp() override {
//         db.init();
//         // 添加测试数据
//         std::vector<Record> test_data = {
//             {1, "Alice", "Engineer"},
//             {2, "Bob", "Designer"},
//             {3, "Charlie", "Manager"},
//             {4, "David", "Developer"},
//             {5, "Eve", "Tester"}
//         };
//         db.add(test_data);
//     }

//     void TearDown() override {
//         db.destory();
//     }
// };

// 测试初始化和基本功能
TEST(DatabaseTest, Initialization) {
    const std::string test_db = "test_db";

    // 测试1：初始化数据库
    SimpleDatabase db(test_db);
    db.init();

    // 测试2：添加数据
    std::vector<Record> records_to_add = {
        {1, "Alice", "Hello World"},
        {2, "Bob", "Hello World"},
        {3, "Charlie", "Hello World"},
        {4, "David", "Hello World"},
        {5, "Eve", "Hello World"}
    };

    bool add_result = db.add(records_to_add);
    Record rec;
    EXPECT_TRUE(db.find_record_by_id(1, rec));
    EXPECT_EQ(rec.name, "Alice");
}

// 测试添加记录
TEST(DatabaseTest, AddRecords) {
    const std::string test_db = "test_db";

    // 测试1：初始化数据库
    SimpleDatabase db(test_db);
    db.init();

    // 测试2：添加数据
    std::vector<Record> records_to_add = {
        {1, "Alice", "Hello World"},
        {2, "Bob", "Hello World"},
        {3, "Charlie", "Hello World"},
        {4, "David", "Hello World"},
        {5, "Eve", "Hello World"}
    };

    std::vector<Record> new_records = {
        {6, "Frank", "Analyst"},
        {7, "Grace", "Architect"}
    };
    EXPECT_TRUE(db.add(new_records));
    
    Record rec6, rec7;
    EXPECT_TRUE(db.find_record_by_id(6, rec6));
    EXPECT_TRUE(db.find_record_by_id(7, rec7));
    EXPECT_EQ(rec6.name, "Frank");
    EXPECT_EQ(rec7.name, "Grace");
}

// 测试点查询
TEST(DatabaseTest, FindById) {
    const std::string test_db = "test_db";

    // 测试1：初始化数据库
    SimpleDatabase db(test_db);
    db.init();

    // 测试2：添加数据
    std::vector<Record> records_to_add = {
        {1, "Alice", "Hello World"},
        {2, "Bob", "Hello World"},
        {3, "Charlie", "Hello World"},
        {4, "David", "Hello World"},
        {5, "Eve", "Hello World"}
    };

    Record rec;
    EXPECT_TRUE(db.find_record_by_id(3, rec));
    EXPECT_EQ(rec.id, 3);
    EXPECT_EQ(rec.name, "Charlie");
    EXPECT_EQ(rec.desc, "Manager");
    
    // 查询不存在的记录
    EXPECT_FALSE(db.find_record_by_id(99, rec));
}

// 测试范围查询
TEST(DatabaseTest, RangeQuery) {
    const std::string test_db = "test_db";

    // 测试1：初始化数据库
    SimpleDatabase db(test_db);
    db.init();

    // 测试2：添加数据
    std::vector<Record> records_to_add = {
        {1, "Alice", "Hello World"},
        {2, "Bob", "Hello World"},
        {3, "Charlie", "Hello World"},
        {4, "David", "Hello World"},
        {5, "Eve", "Hello World"}
    };

    auto result = db.find_records_in_range(2, 5); // id 2,3,4
    EXPECT_EQ(result.size(), 3);
    EXPECT_EQ(result[0].id, 2);
    EXPECT_EQ(result[1].id, 3);
    EXPECT_EQ(result[2].id, 4);
    
    // 空范围查询
    auto empty_result = db.find_records_in_range(10, 20);
    EXPECT_TRUE(empty_result.empty());
}

// 测试前缀查询
TEST(DatabaseTest, PrefixQuery) {
    const std::string test_db = "test_db";

    // 测试1：初始化数据库
    SimpleDatabase db(test_db);
    db.init();

    // 测试2：添加数据
    std::vector<Record> records_to_add = {
        {1, "Alice", "Hello World"},
        {2, "Bob", "Hello World"},
        {3, "Charlie", "Hello World"},
        {4, "David", "Hello World"},
        {5, "Eve", "Hello World"}
    };

    auto result = db.find_records_by_name_prefix("A"); // Alice
    EXPECT_EQ(result.size(), 1);
    EXPECT_EQ(result[0].id, 1);
    
    auto result_b = db.find_records_by_name_prefix("B"); // Bob
    EXPECT_EQ(result_b.size(), 1);
    EXPECT_EQ(result_b[0].id, 2);
    
    // 不存在的前缀
    auto no_result = db.find_records_by_name_prefix("X");
    EXPECT_TRUE(no_result.empty());
}

// 测试更新记录
TEST(DatabaseTest, UpdateRecord) {
    const std::string test_db = "test_db";

    // 测试1：初始化数据库
    SimpleDatabase db(test_db);
    db.init();

    // 测试2：添加数据
    std::vector<Record> records_to_add = {
        {1, "Alice", "Hello World"},
        {2, "Bob", "Hello World"},
        {3, "Charlie", "Hello World"},
        {4, "David", "Hello World"},
        {5, "Eve", "Hello World"}
    };

    EXPECT_TRUE(db.update_name(2, "Robert"));
    
    Record rec;
    EXPECT_TRUE(db.find_record_by_id(2, rec));
    EXPECT_EQ(rec.name, "Robert");
    
    // 更新不存在的记录
    EXPECT_FALSE(db.update_name(99, "Test"));
}

// 测试删除记录
TEST(DatabaseTest, DeleteRecords) {
    const std::string test_db = "test_db";

    // 测试1：初始化数据库
    SimpleDatabase db(test_db);
    db.init();

    // 测试2：添加数据
    std::vector<Record> records_to_add = {
        {1, "Alice", "Hello World"},
        {2, "Bob", "Hello World"},
        {3, "Charlie", "Hello World"},
        {4, "David", "Hello World"},
        {5, "Eve", "Hello World"}
    };

    EXPECT_TRUE(db.remove({1, 3}));
    
    Record rec1, rec3;
    EXPECT_FALSE(db.find_record_by_id(1, rec1));
    EXPECT_FALSE(db.find_record_by_id(3, rec3));
    
    // 确认其他记录不受影响
    Record rec2, rec4;
    EXPECT_TRUE(db.find_record_by_id(2, rec2));
    EXPECT_TRUE(db.find_record_by_id(4, rec4));
}

// 测试清空数据库
TEST(DatabaseTest, Truncate) {
    const std::string test_db = "test_db";

    // 测试1：初始化数据库
    SimpleDatabase db(test_db);
    db.init();

    // 测试2：添加数据
    std::vector<Record> records_to_add = {
        {1, "Alice", "Hello World"},
        {2, "Bob", "Hello World"},
        {3, "Charlie", "Hello World"},
        {4, "David", "Hello World"},
        {5, "Eve", "Hello World"}
    };

    EXPECT_TRUE(db.truncate());
    auto result = db.find_records_in_range(1, 10);
    EXPECT_TRUE(result.empty());
}

// 测试多次初始化
TEST(DatabaseTest, MultipleInitialization) {
    const std::string test_db = "test_db";

    // 测试1：初始化数据库
    SimpleDatabase db(test_db);
    db.init();

    // 测试2：添加数据
    std::vector<Record> records_to_add = {
        {1, "Alice", "Hello World"},
        {2, "Bob", "Hello World"},
        {3, "Charlie", "Hello World"},
        {4, "David", "Hello World"},
        {5, "Eve", "Hello World"}
    };

    db.destory();
    db.init();
    
    // 初始化后数据库应为空
    auto result = db.find_records_in_range(1, 10);
    EXPECT_TRUE(result.empty());
    
    // 重新添加记录
    std::vector<Record> new_data = {{10, "Test", "Test"}};
    db.add(new_data);
    
    Record rec;
    EXPECT_TRUE(db.find_record_by_id(10, rec));
}

int main(int argc, char **argv) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
